package com.foxitcloud.webpdf.calldll;

import org.apache.commons.lang3.mutable.MutableLong;

public class FXPDFFormHandle {
	private long handle = 0;

	public FXPDFFormHandle(long handle) {
		this.handle = handle;
	}

	protected void finalize() {
		release();
	}

	/**
	 * Release all resource. Note: this function must be call after use
	 * FXPDFFormHandle, otherwise there will be memory leak.
	 */
	public void release() {
		Na_release(this.handle);
		this.handle = 0;
	}

	/**
	 * Parse a PDF document.
	 * 
	 * @param pdfFilePath
	 *            path of the document.
	 * @param pdfPassWord
	 *            password of PDF document.
	 * @return error code.
	 * @see FXPDFError
	 */
	public long parsePDFDocument(String pdfFilePath, String pdfPassWord) {
		return Na_parsePDFDocument(this.handle, pdfFilePath, pdfPassWord);
	}

	/**
	 * Parse a PDF document.
	 * 
	 * @param pdfStream
	 *            data of PDF file.
	 * @param pdfPassWord
	 *            password of PDF document.
	 * @return error code.
	 * @see FXPDFError
	 */
	public long parsePDFDocument(final byte[] pdfStream, String pdfPassWord) {
		return Na_parsePDFStream(this.handle, pdfStream, pdfPassWord);
	}

	/**
	 * Get page count of the document.
	 * 
	 * @return page count of the document.
	 */
	public int getPageCount() {
		return Na_getPageCount(this.handle);
	}

	/**
	 * Generate form information.
	 * 
	 * @param pageIndex
	 *            page index.
	 * @param createImage
	 *            whether to create background image.
	 * @param result
	 *            status.
	 * @param xmlInfoBuffer
	 *            String buffer to receive XML information of current page.
	 * @return background image data if the parameter <l>createImage</l> is set
	 *         to true, otherwise return false.
	 * @see FXPDFError
	 */
	public byte[] outputBGImageToBuf(int pageIndex, boolean createImage,
			StringBuffer xmlInfoBuffer, MutableLong result) {
		Long ret = (long) 0;
		xmlInfoBuffer.setLength(0);
		byte[] imageStream = Na_outputBGImage2Buf(this.handle, pageIndex,
				createImage, xmlInfoBuffer, ret);
		result.setValue(ret);
		if (ret != 0)
			return null;
		return imageStream;
	}

	/**
	 * call this function after outputBGImageToBuf to get form information in
	 * XML format.
	 * 
	 * @param ret
	 *            status.
	 * @return form information in XML format.
	 * @see FXPDFError
	 */
	public String getFromXmlInfo(MutableLong result) {
		Long ret = (long) 0;
		String text = Na_getFormXml(this.handle, ret);
		result.setValue(ret);
		if (ret != 0)
			return "";
		return text;
	}

	/**
	 * Import from data to PDF file.
	 * 
	 * @param xmlData
	 *            form data in XML format.
	 * @return status.
	 * @see FXPDFError
	 */
	public int importFromXMLFile(byte[] xmlData) {
		if (xmlData == null)
			return -1;
		int ret = Na_importFromXMLFile(this.handle, xmlData);
		return ret;
	}

	/**
	 * Export form data to FDF format.
	 * 
	 * @param ret
	 *            status.
	 * @return form data in FDF format.
	 * @see FXPDFError
	 */
	public byte[] exportToFDF(Integer ret) {
		ret = (int) 0;
		byte[] fdfData = Na_exportToFDF(this.handle, ret);
		if (ret != 0)
			return null;
		return fdfData;
	}

	/**
	 * Export form data to XML format.
	 * 
	 * @param isAddFormatInfo
	 *            whether add format information of form field. always set to
	 *            false.
	 * @param ret
	 *            status.
	 * @return form data in XML format.
	 * @see FXPDFError
	 */
	public byte[] exportToXML(boolean isAddFormatInfo, Integer ret) {
		ret = (int) 0;
		byte[] fdfData = Na_exportToXML(this.handle, isAddFormatInfo, ret);
		if (ret != 0)
			return null;
		return fdfData;
	}

	/**
	 * Save PDF document.
	 * 
	 * @param ret
	 *            status.
	 * @return saved document data.
	 * @see FXPDFError
	 */
	public byte[] saveFileAs(Integer ret) {
		ret = (int) 0;
		byte[] pdfData = Na_saveFileAs(this.handle, ret);
		if (ret != 0)
			return null;
		return pdfData;
	}

	protected native void Na_release(long handle);

	protected native long Na_parsePDFDocument(long handle, String pdfFilePath,
			String pdfPassWord);

	protected native long Na_parsePDFStream(long handle,
			final byte[] pdfStream, String pdfPassWord);

	protected native int Na_getPageCount(long handle);

	protected native byte[] Na_outputBGImage2Buf(long handle, int pageIndex,
			boolean createImage, StringBuffer xmlInfoBuffer, Long ret);

	protected native String Na_getFormXml(long handle, Long ret);

	protected native int Na_importFromXMLFile(long handle, final byte[] xmlData);

	protected native byte[] Na_exportToFDF(long handle, Integer ret);

	protected native byte[] Na_exportToXML(long handle,
			boolean isAddFormatInfo, Integer ret);

	protected native byte[] Na_saveFileAs(long handle, Integer ret);
}
